# -*- coding: UTF-8 -*-
# 2023/11/16 13:14
import os.path

from ttkbootstrap import Window, Combobox, Style, Label, Frame, Button
from ttkbootstrap.tooltip import ToolTip

from computer.computer_top import computer_top
from phone.phone_top import phone_wallpaper
from tools.get_configer import my_configer
from tools.get_icon import mobile_phone, computer, logo
from tools.get_logger import get_logger
from tools.img_tool import set_pil_img, set_tkimage

configer = my_configer()
loger = get_logger()


class root(Window):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.theme_style = Style()
        self.select_theme = configer.get("main", "style")
        self.show_computer = False
        self.show_phone = False

        loger.info(f"读取配置同步主题:{self.select_theme}")

        self.fps = 60  # 设置的帧率
        self.frame_time = int(1000 / self.fps)  # 每帧的毫秒数
        self.update_counter()

        self.mk_dir()
        self.windows_init()
        self.setupUI()
        self.setlayout()
        self.setCallback()
        self.setBalloon()

    def update_counter(self):
        self.after(self.frame_time, self.update_counter)

    def windows_init(self):
        """
        窗口配置
        """
        self.title("壁纸自由")
        self.resizable(False, False)

        self.iconphoto(False, set_tkimage(logo, self))
        self.place_window_center()  # 窗口居中,忽略标题栏高度

        loger.info(f"设置窗口不可调节,设置窗口图标,设置窗口居中")

    def mk_dir(self):
        """
        创建必须文件夹
        """

        if not os.path.exists("./cache"):
            os.mkdir("./cache")
            os.mkdir("./cache/computer_cache")
            os.mkdir("./cache/phone_cache")
            os.mkdir("./cache/computer_cover")
            os.mkdir("./cache/phone_cover")

            loger.info(f"创建所有缓存目录.")

        else:
            if not os.path.exists("./cache/computer_cache"):
                os.mkdir("./cache/computer_cache")
            if not os.path.exists("./cache/phone_cache"):
                os.mkdir("./cache/phone_cache")
            if not os.path.exists("./cache/computer_cover"):
                os.mkdir("./cache/computer_cover")
            if not os.path.exists("./cache/phone_cover"):
                os.mkdir("./cache/phone_cover")

    def setupUI(self):
        """
        创建组件
        """
        self.theme_Frame = Frame(self)
        self.type_frame = Frame(self)

        # 选择主题区域---------------------------------------------------
        self.theme_names = Style().theme_names()  # ['cosmo', 'flatly', 'litera', 'minty', 'lumen', 'sandstone', 'yeti', 'pulse', 'united', 'morph', 'journal', 'darkly', 'superhero', 'solar', 'cyborg', 'vapor', 'simplex', 'cerculean']
        self.style_Label = Label(self.theme_Frame, text=self.select_theme, font="-size 24 -weight bold")
        self.style_Combobox = Combobox(self.theme_Frame, state="readonly", values=self.theme_names)
        # 设置默认主题
        ini_theme_index = self.theme_names.index(self.select_theme)
        self.style_Combobox.current(ini_theme_index)
        self.theme_style.theme_use(self.select_theme)

        loger.info(f"选择主题区域组件生成")

        # 选择壁纸类型区域-----------------------------------------------
        # 两个壁纸按钮选项
        self.mobile_phone_image = set_pil_img(mobile_phone, (256, 256))
        self.phone_Button = Button(self.type_frame, text="竖屏壁纸", image=self.mobile_phone_image, compound='bottom')

        self.computer_image = set_pil_img(computer, (256, 256))
        self.computer_Button = Button(self.type_frame, text="横屏壁纸", image=self.computer_image, compound='bottom')

        loger.info(f"选择壁纸类型区域组件生成")

    def setCallback(self):
        """
        设置组件回调方法
        """
        self.style_Combobox.bind('<<ComboboxSelected>>', self.update_themes)  # 更新主题选择回调
        self.computer_Button.bind("<ButtonRelease-1>", self.show_computer_Top)  # 电脑壁纸按钮回调
        self.phone_Button.bind("<ButtonRelease-1>", self.show_phone_Top)  # 手机壁纸按钮回调

        loger.info(f"分别为主题选择下拉框,电脑手机壁纸按钮绑定回调方法.")

    def setlayout(self):
        """
        组件布局
        """
        self.theme_Frame.pack()
        self.type_frame.pack()

        self.style_Label.pack(padx=10, side="left")
        self.style_Combobox.pack(padx=10, side="left")

        self.computer_Button.pack(padx=10, side="left")
        self.phone_Button.pack(padx=10, side="left")

        loger.info(f"主窗口组件布局")

    def setBalloon(self):
        """
        设置组件提示气泡
        """
        ToolTip(self.style_Combobox, text="点击选择切换主题颜色")
        ToolTip(self.computer_Button, "点击选择电脑壁纸")
        ToolTip(self.phone_Button, "点击选择手机壁纸")

        loger.info(f"主窗口组件设置提示")

    def update_themes(self, event):
        """
        更新全局主题
        """
        self.select_theme = self.style_Combobox.get()
        self.theme_style.theme_use(self.select_theme)
        self.style_Label.configure(text=self.select_theme)

        # 更新配置,方法自动保存配置
        configer.set("main", "style", self.select_theme)
        loger.info(f"选择主题更新,全局主题更新为{self.select_theme}")

    def show_computer_Top(self, event):
        """
        显示电脑壁纸窗口
        """
        if not self.show_computer:
            self.computer_Top = computer_top(master=self, title="电脑壁纸", minsize=(600, 600), resizable=(False, False))
            self.computer_Top.protocol('WM_DELETE_WINDOW', self.close_computer_Top)  # 电脑壁纸窗口关闭回调
            self.show_computer = True

            loger.info(f"打开电脑壁纸窗口")

            self.iconify()  # 最小化主窗口

    def close_computer_Top(self):
        """
        关闭电脑壁纸窗口,恢复标识
        """
        self.show_computer = False
        self.computer_Top.destroy()  # 销毁电脑窗口

        loger.info(f"销毁电脑壁纸窗口")
        self.deiconify()  # 重新显示主窗口

    def show_phone_Top(self, event):
        """
        显示手机壁纸窗口
        """
        if not self.show_phone:
            self.phone_Top = phone_wallpaper(master=self, title="手机壁纸", minsize=(600, 600), resizable=(True, True))
            self.phone_Top.protocol('WM_DELETE_WINDOW', self.close_phone_Top)  # 手机壁纸窗口关闭回调
            self.show_phone = True

            loger.info(f"打开手机壁纸窗口")
            self.iconify()  # 最小化主窗口

    def close_phone_Top(self):
        """
        关闭手机壁纸窗口,恢复标识
        """
        self.show_phone = False
        self.phone_Top.destroy()  # 销毁手机窗口

        loger.info(f"销毁手机壁纸窗口")
        self.deiconify()  # 重新显示主窗口


if __name__ == '__main__':
    root().mainloop()
